﻿using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MySqlX.XDevAPI.Common;
using Namotion.Reflection;
using Photo.Core.Dtos.Dto;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Photo.Core.SeedData
{
    /// <summary>
    /// 系统接口权限-预置数据
    /// </summary>
    public class SysSecuritySeedData
    {
        /// <summary>
        /// 获取系统接口鉴权api
        /// </summary>
        /// <returns></returns>
        public static List<SysSecurityDto> GetSeedData()
        {
            //获取Application项目下的所有动态webapi继承来的类
            var assembly = Assembly.Load("Photo.Application").GetTypes().AsEnumerable()
                .Where(type => typeof(IDynamicApiController).IsAssignableFrom(type)).ToList();

            List<SysSecurityDto> list = new List<SysSecurityDto>();
            assembly.ForEach(r =>
            {
                foreach (var methodInfo in r.GetMethods())
                {
                    if (methodInfo.IsPublic)
                    {
                        var securityDefineAttribute = methodInfo.GetCustomAttribute<SecurityDefineAttribute>();
                        var apiDescriptionSettingsAttribute = methodInfo.DeclaringType.GetCustomAttribute<ApiDescriptionSettingsAttribute>();

                        //纳入角色权限，必须同时冠有SecurityDefineAttribute和ApiDescriptionSettingsAttribute
                        if (securityDefineAttribute != null && apiDescriptionSettingsAttribute != null)
                        {
                            //接口描述
                            var description = methodInfo.GetXmlDocsSummary();
                            var tag = methodInfo.DeclaringType.Name.Replace("Service", "");
                            //接口路由
                            var route = string.Format("/api/{0}/{1}/{2}", apiDescriptionSettingsAttribute.GroupName, tag, methodInfo.Name).ToLower();

                            list.Add(new SysSecurityDto
                            {
                                Route = route,
                                Description = description,
                                Group = apiDescriptionSettingsAttribute.GroupName,
                                Tag = tag,
                            });
                        }

                    }
                }
            });

            return list;
        }

        /// <summary>
        /// 获取dtree格式的系统接口数据
        /// </summary>
        /// <returns></returns>
        public static List<EleTreeDto> GetEleTreeList()
        {
            var list = GetSeedData().Select(o => new EleTreeDto
            {
                Id = o.Route,
                Label =  $"{o.Route}  {o.Description}",
                ParentId = $"{o.Group}.{o.Tag}"?.ToLower()
            })
            .ToList();
            //为接口添加顶部分组父级数据
            var parentGroupList = list.GroupBy(o => o.ParentId)
                .Select(o => new EleTreeDto
                {
                    Id = o.Key,
                    Label = o.Key,
                    ParentId = "0"
                }).ToList();
            list.InsertRange(0, parentGroupList);

            return list.ToTree("Id", "ParentId", "Children", "0");
        }
    }
}
